<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>秒杀管理</el-breadcrumb-item>
      <el-breadcrumb-item>秒杀列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getSeckills">
            <el-button slot="append" icon="el-icon-search" @click="getSeckills"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="AdddialogVisible = true">添加秒杀活动</el-button>
          <el-button type="danger" @click="delGroup" :disabled="this.sels.length === 0">批量删除</el-button>
        </el-col>
      </el-row>

      <!-- 列表渲染 -->
      <el-table :data="seckills" border stripe @selection-change="selsChange">
        <el-table-column type="selection" width="55"></el-table-column>

        <el-table-column label="活动名称" prop="sk_title"></el-table-column>

        <el-table-column label="开始时间" prop="start_time" width="220px">
          <template slot-scope="scope">{{scope.row.start_time|dateFormat}}</template>
        </el-table-column>
        <el-table-column label="结束时间" prop="end_time" width="220px">
          <template slot-scope="scope">{{scope.row.end_time|dateFormat}}</template>
        </el-table-column>

        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status==1?true:false" @change="statusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>

        <el-table-column>
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEdit(scope.row.id)"
            ></el-button>
            <el-tooltip popper-class="tooltip" placement="top">
              <el-button
                type="success"
                icon="el-icon-upload el-icon"
                size="mini"
                @click="showAdd(scope.row.id)"
              ></el-button>
              <div slot="content">
                <div>添加活动商品</div>
              </div>
            </el-tooltip>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="remove(scope.row.id)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1,2,5,10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加 -->
    <el-dialog title="添加秒杀活动" :visible.sync="AdddialogVisible" width="50%" @close="AddDialogClosed">
      <el-form :model="Addseckill" :rules="seckillRules" ref="seckillRef" label-width="80px">
        <el-form-item label="名称" prop="sk_title">
          <el-input v-model="Addseckill.sk_title"></el-input>
        </el-form-item>
        <el-form-item label="是否上线">
          <el-radio-group v-model="Addseckill.status">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否首推">
          <el-radio-group v-model="Addseckill.recommended">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开始时间" prop="start_time" width="220px">
          <el-date-picker
            v-model="Addseckill.start_time"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="end_time" width="220px">
          <el-date-picker
            v-model="Addseckill.end_time"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AdddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="修改秒杀活动"
      :visible.sync="EditdialogVisible"
      width="50%"
      @close="EditDialogClosed"
    >
      <el-form :model="Editseckill" :rules="seckillRules" ref="seckillRef" label-width="80px">
        <el-form-item label="名称" prop="sk_title">
          <el-input v-model="Editseckill.sk_title"></el-input>
        </el-form-item>
        <el-form-item label="是否上线">
          <el-radio-group v-model="Editseckill.status">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否首推">
          <el-radio-group v-model="Editseckill.recommended">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="期限">{{this.start_time|dateFormat}} 至 {{this.end_time|dateFormat}}</el-form-item>
        <el-form-item label="开始时间" prop="start_time" width="220px">
          <el-date-picker
            v-model="Editseckill.start_time"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="end_time" width="220px">
          <el-date-picker
            v-model="Editseckill.end_time"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="EditdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { server } from '../api/index'
export default {
  data() {
    return {
      seckills: [],
      sels: [], //选中的值显示
      //获取用户列表参数对象
      queryInfo: {
        query: '',
        //当前页数
        pagenum: 1,
        //当前页显示多少条

        pagesize: 10
      },
      total: 0,
      AdddialogVisible: false,
      EditdialogVisible: false,
      start_time: 0,
      end_time: 0,
      Addseckill: {
        sk_title: '',
        start_time: 0,
        end_time: 0,
        status: '0',
        recommended: '0'
      },
      Editseckill: {
        sk_title: '',
        start_time: 0,
        end_time: 0,
        status: '0',
        recommended: '0'
      },
      seckillRules: {
        sk_title: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        start_time: [
          { required: true, message: '请输入开始时间', trigger: 'blur' }
        ],
        end_time: [
          { required: true, message: '请输入结束时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getSeckills()
  },
  methods: {
    async getSeckills() {
      const { data: res } = await server.getSeckill(this.queryInfo)
      if (res.code !== 200) {
        return this.$message.error('获取失败')
      }

      this.seckills = res.data.data
      this.total = res.data.total
      console.log(this.seckills)
    },
    selsChange(sels) {
      this.sels = sels
    },
    //监听分页改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getSeckills()
    },
    //监听页码值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getSeckills()
    },
    AddDialogClosed() {
      this.$refs.seckillRef.resetFields()
    },
    EditDialogClosed() {
      this.$refs.seckillRef.resetFields()
    },
    add() {
      this.$refs.seckillRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项')
        }
        var start = this.Addseckill.start_time.toString()
        var start = start.slice(0, 10)
        this.Addseckill.start_time = parseInt(start)
        var end = this.Addseckill.end_time.toString()
        var end = end.slice(0, 10)
        this.Addseckill.end_time = parseInt(end)
        console.log(this.Addseckill)
        const { data: res } = await server.addSeckill(this.Addseckill)
        if (res.code !== 200) {
          return this.$message.error('添加失败')
        }
        this.getSeckills()
        this.$message.success('添加成功')
        this.AdddialogVisible = false
      })
    },

    remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async delete(id) {
      const { data: res } = await server.delSeckill(id)

      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      //刷新列表
      this.getSeckills()
      this.$message.success('删除成功！')
    },
    delGroup() {
      var ids = this.sels.map(item => item.id).join() //获取所有选中行的id组成的字符串，以逗号分隔

      this.$confirm('此操作将永久删除选中文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delAll(ids)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async delAll(ids) {
      const { data: res } = await server.delAllSeckill(ids)

      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      //刷新列表
      this.getSeckills()
      this.$message.success('删除成功！')
    },
    async showEdit(id) {
      const { data: res } = await server.detailSeckill(id)

      if (res.code !== 200) {
        return this.$message.error('查询失败！')
      }
      this.Editseckill = res.data
      this.Editseckill.status = res.data.status.toString()
      this.Editseckill.recommended = res.data.recommended.toString()
      this.start_time = this.Editseckill.start_time
      this.end_time = this.Editseckill.end_time
      this.EditdialogVisible = true
    },
    edit(id) {
      this.$refs.seckillRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项')
        }
        var start = this.Editseckill.start_time.toString()
        var start = start.slice(0, 10)
        this.Editseckill.start_time = this.start_time = parseInt(start)
        var end = this.Editseckill.end_time.toString()
        var end = end.slice(0, 10)
        this.Editseckill.end_time = this.end_time = parseInt(end)
        const { data: res } = await server.editSeckill(id, this.Editseckill)
        if (res.code !== 200) {
          return this.$message.error('修改失败')
        }
        this.getSeckills()
        this.$message.success('修改成功')
        this.EditdialogVisible = false
      })
    },
    //监听switch开关的状态
    async statusChange(seckills) {
      // console.log(userinfo)
      const { data: res } = await server.statusSeckill(
        seckills.id,
        seckills.status
      )
      // console.log(res.data)
      if (res.code !== 200) {
        seckills.status = !seckills.status
        return this.$message.error('更新状态失败')
      }
      seckills.status = !seckills.status

      this.$message.success('更新状态成功')
    },
    showAdd(id) {
      this.$router.push('/seckill/goods/' + id)
    }
  }
}
</script>
<style scoped>
</style>